function add_coupon(){
	jQuery.post(BASE_URI+'admin/dataman/coupon/add_coupon',{},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:300,
				 title: 'Add coupon' 
			}
		);
	});	
}

function edit_coupon(id){
	jQuery.post(BASE_URI+'admin/dataman/coupon/add_coupon',{id:id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:300,
				 title: 'Edit coupon' 
			}
		);
	});	
}

function view_users(id){
	jQuery.post(BASE_URI+'admin/dataman/coupon/view_users',{id:id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Users redeem coupon' 
			}
		);
	});	
}

function delete_coupon(id){
	jQuery.post(BASE_URI+'admin/dataman/coupon/delete_coupon',{id:id},function(res){
		reload();
	});
}

jQuery(document).ready(function() {
	var optionsEmailSubs = { 
		beforeSubmit:beforeSubmit,   
		success:afterSubmit
	};	
	
	jQuery("#add_coupon_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsEmailSubs);
		return false;
	});	
		
});

jQuery(function() {
	
	jQuery("#start_time_btn").live('click',function(){
		jQuery('.DynarchCalendar-topCont').css({top:100,width:60});
		Calendar.setup({
			inputField : "start_time",
			trigger    : "start_time_btn",
			onSelect   : function() { this.hide() },
			showTime   : 24,
			dateFormat : "%Y-%m-%d %I:%M"
		});
	});
	jQuery("#end_time_btn").live('click',function(){
		jQuery('.DynarchCalendar-topCont').css({top:100,width:60});
		Calendar.setup({
			inputField : "end_time",
			trigger    : "end_time_btn",
			onSelect   : function() { this.hide() },
			showTime   : 24,
			dateFormat : "%Y-%m-%d %I:%M"
		});
	});
	
});

function beforeSubmit(formData, jqForm, options){
	jQuery('#loader_image').show();
	return true;
}

function afterSubmit(responseText, statusText, xhr, $form){
	jQuery('#loader_image').hide();	
	 
	if(responseText == 'ok'){
		jQuery('#dialog').dialog('close');
		reload();
	}else{
		alert(responseText);
	}
}